// pages/new_patient/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
     id:0,
     medical_username:'',
     medical_gender:'',
     medical_cardno:'',
     medical_birth:'',
     is_submit:false
    },
    genderBirth(e) {
        this.setData({
            'medical_birth':e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       if(options.id){
           this.setData({
               id:options.id
           })
        wx.setNavigationBarTitle({ title: '编辑就诊人信息'})
        this.getDetail()
       }       
    },
    getDetail()
    {
        var that = this;
        var json = {};
       
       json.id=that.data.id
        request.requestLoading(
            "/api/mine/mineInfoDetail",
            json,
            'GET',
          ).then((res) => {
           console.log(res.data)
        that.setData({
            medical_cardno:res.data.medical_cardno,
            medical_gender:res.data.medical_gender,
            medical_username:res.data.medical_username,
            medical_birth:res.data.medical_birth
        })
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    inputCard(e){
     this.setData({
         medical_cardno:e.detail.value
     })
     console.log(this.data.medical_cardno)
    },
    inputUsername(e){
        this.setData({
            medical_username:e.detail.value
        })
        console.log(this.data.medical_username)
       },
       choosegender(e){
        console.log(e)
        var  gender = e.currentTarget.dataset.gender;
        this.setData({
            medical_gender:gender
        })
    },
    submit()
    {
        var that = this;
        var json = {};
        json.info_id =that.data.id;
        json.medical_username=that.data.medical_username;
        json.medical_gender=that.data.medical_gender;
        json.medical_cardno=that.data.medical_cardno;
        json.medical_birth=that.data.medical_birth;
        console.log(json)
       if(json.medical_username==''){
           request.toast('请填写就诊人姓名');
           return false;
       }
       if(json.medical_gender==''){
        request.toast('请填写性别');
        return false;
    }
    if(!json.medical_birth){
        request.toast('请选择生日');
        return false;
    }
    // if(json.medical_cardno==''){
    //     request.toast('请填写身份证号');
    //     return false;
    // }
    if(that.data.is_submit){
      wx.showToast({
        title: '请勿重复提交',
        icon:'none'
      })
      return false
    }
    that.setData({
        is_submit:true
    })
        request.requestLoading(
            "/api/mine/saveInfo",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
              wx.navigateBack({
                  delta:1
              })
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
                that.setData({
                    is_submit:false
                })
            }
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

  
})